home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Libris Britannia 4
/
science library(b).zip
/
science library(b)
/
PROGRAMM
/
CC_C
/
0151.ZIP
/
TF.C
< prev
next >
Wrap
C/C++ Source or Header
|
1985-10-25
|
16KB
|
638 lines
/********************************************************
* *
* T E X T F O R M *
* Text file formatter for MSDOS *
* *
* T. Jennings 24 Oct 85 *
* created 14 Nov. 82 *
* *
*********************************************************
TEXTFORM prints and/or formats edited text, using WordStar
compatible dot commands, for use on non-WordStar systems. See
TEXTFORM.DOC for details. */
#include <ascii.h>
char *next_arg();
char *skip_delim();
char *strip_path();
#define FALSE 0
#define TRUE 1
#define ERROR -1
#define BOLD 2 /* ^B bold on/off, */
#define UNDER 19 /* ^S understrike, */
#define DOUBLE 4 /* ^D double strike, */
#define STRIKE 24 /* ^X strikeout */
int line_count; /* total lines, */
int this_line; /* current line #/page */
int this_page; /* current page */
int abs_page; /* physical page */
int this_col;
int pagelen; /* current max page length */
int lmargin,rmargin; /* current left and right margins */
int top_margin; /* lines from top of page */
int bot_margin; /* lines at bottom of page */
int num_pages; /* true if do page numbers */
int csource; /* true if paging C sources, */
int brackets; /* bracket nesting level */
int newpage; /* time for a new page, */
int lineno; /* true if doing line numbers */
int title; /* true if top of page titles */
int first_page; /* first page to print, */
int infile; /* main text file */
int auxfile; /* include file */
int outfile;
char auxname[80];
char inname[80]; /* where we save ASCII filenames */
char outname[80];
char header[134]; /* top of page header */
char subheader[134]; /* sub title, */
/* Control characters are used to specify a character string; these
perform various definable functions. The WS functions ^B, ^D, ^S, etc
are done explicitly. */
#define CTLMAX 8 /* max size of string */
char ctlstr[32][CTLMAX + 1] = { /* control character macros */
"", /* NUL, not accessible */
"\033W0", /* ESC W 0 Okidata double wide off */
"\033G", /* ESC G Okidata emphasized on */
"\033H", /* ESC H Okidata emphasized off */
"", /* ^D */
"\033I\003", /* ESC I ^C Okidata CQ mode */
"\033I\001", /* ESC I ^A Okidata DP mode */
"\033W1", /* ESC W 1 Okidata double wide on */
"\010", /* BS */
"\011", /* HT */
"\012", /* LF */
"", /* ^K used by Index */
"\014", /* FF */
"\015", /* CR */
"\033%G", /* ESC % G Okidata italicized on */
"\033%H", /* ESC % H Okidata italicized off */
"", /* ^P used by Index */
"\0330", /* ESC 0 Okidata 8 LPI */
"\033A\014\0332", /* ESC A ^L ESC 2 Okidata 6 LPI */
"\033-\001", /* ESC - ^A Okidata underline on */
"\033-\200", /* ESC - ^@ (null) underline off */
"\022", /* NAK Okidata 10CPI */
"\033:", /* ESC : Okidata 12CPI */
"\017", /* ETB Okidata 17.1CPI */
"",
"",
"",
"",
"",
"",
"",
""
};
int graph_flag; /* 1 == do output graphics */
#define MAXMEM 65535L /* max we will allocate (note 64K - 1) */
#define MINMEM (1024L * 3L) /* the minimum */
long memsize;
char *inbuf; /* input file buffer */
unsigned ibufsize; /* how big it is, */
unsigned ibufcount; /* how much is in it */
unsigned ibufindex;
char *outbuf; /* output buffer */
unsigned obufsize;
unsigned obufcount;
unsigned obufindex;
char *auxbuf;
unsigned abufsize;
unsigned abufcount;
unsigned abufindex;
static int _stack = 15000;
char *getmem();
long sizmem();
/* Process the input file. If no output is specified, write it to
the standard output. */
main(argc,argv)
int argc;
char *argv[];
{
char *p,arg[80],sw[80];
int i,x;
int have_file;
int arg_error;
printf ("TF - Text Formatter - (c) Fido Software 24 Oct 85\r\n");
allmem();
memsize= sizmem();
if (memsize < MINMEM) {
printf("Go buy more memory.\r\n");
exit(1);
}
if (memsize > MAXMEM) memsize= MAXMEM;
/* Split the buffer into three pieces, input, aux input and output. */
obufsize= 1024L;
ibufsize= abufsize= ((memsize - obufsize) / 2L);
outbuf= getmem( (int) memsize);
inbuf= &outbuf[obufsize];
auxbuf= &inbuf[ibufsize];
ibufcount= ibufindex= 0;
obufcount= obufindex= 0;
abufcount= abufindex= 0;
printf("Input Buffer : %8d bytes\r\n",ibufsize);
printf(".IN File Buffer : %8d bytes\r\n",abufsize);
printf("Output Buffer : %8d bytes\r\n",obufsize);
newdelim(" \t\r\n;,"); /* arg delimiter list, */
pagelen= 66; /* page length */
lmargin= 8; /* left margin */
top_margin= 1; /* top margin */
bot_margin= 8; /* bottom margin */
*header= '\0';
*subheader= '\0';
num_pages= 1; /* do auto page numbers */
graph_flag= 1; /* do graphics */
have_file= 0; /* no output file yet */
arg_error= 0; /* no error yet, */
csource= 0; /* not a C source, */
title= 0; /* no title yet, */
lineno= 0; /* no line numbers yet */
brackets= 0;
newpage= 0;
auxfile= 0; /* not including */
first_page= 1;
num_pages= 10000;
strcpy(inname,""); /* no file yet, */
strcpy(outname,"prn"); /* default to printer */
/* Process the command line. We must have a filename, and optionally one
of two options: list amount of memory, of add line numbers to the index. */
x= 0; /* no filename yet */
while (--argc) {
++argv;
cpyarg(arg,*argv); /* possible name, */
if ((x == 0) && (strlen(arg) > 0)) { /* if no name yet, */
strcpy(inname,arg); /* get one, */
x= 1;
}
strip_switch(sw,*argv); /* get the switches, */
p= sw;
while (*p) {
if (*p == 'L') {
lineno= 1;
printf(" - Line numbers\r\n");
} else if (*p == 'U') {
graph_flag= 0;
printf(" - No output graphics\r\n");
} else if (*p == 'C') {
csource= 1;
printf(" - C Source\r\n");
} else if (*p == 'T') {
title= 1;
printf(" - Top of page Titles\r\n");
} else if (*p == 'O') {
strcpy(outname,arg);
printf(" - Output to %s\r\n",outname);
} else if (*p == 'P') {
first_page= atoi(arg);
if (first_page < 1) first_page= 1;
printf("- Start Printing at Page #%u\r\n",first_page);
} else if (*p == 'N') {
num_pages= atoi(arg);
if (num_pages < 1) num_pages= 1;
printf("- Print Only %u Pages\r\n",num_pages);
} else {
printf("* I don't know what '%c' is\r\n",*p);
++arg_error;
}
++p;
}
}
if (*inname == '\0') {
printf("* Missing a File to Print!\r\n");
++arg_error;
} else {
infile= _xopen(inname,0); /* open file, */
if (infile == -1) {
printf("\r\n* Can't find %s\r\n",inname);
++arg_error;
} else {
outfile= _xcreate(outname,2);
if (outfile == -1) {
printf("\r\n* Can't write to %s\r\n",outname);
++arg_error;
}
}
}
if (arg_error) {
printf("\r\nTry: TF {switches} filename {outfile/O} where\r\n");
printf("* filename is the file to print,\r\n");
printf("* outfile/O is where to print to (default is PRN)\r\n");
printf("* switches are: /C C source, /T Page titles, /L Line numbers\r\n");
printf("* x/P Start at page X, x/N Print X pages, /U no output graphics.\r\n");
exit(1);
}
if (csource) {
lmargin= 0;
}
print();
while ((this_line > 0) && (this_line < pagelen))
lprintf("\r\n"); /* go to top of form */
wflush();
_xclose(infile);
_xclose(outfile);
exit(0); /* exit. */
}
/* Print each line to the printer, processing dot commands as we go. */
print()
{
char linebuf[132]; /* character line buffer */
int i;
char linenum[20];
int last_page;
line_count= 0;
this_page= abs_page= 1;
this_line= 0;
this_col= 0;
newpage= 0;
last_page= -1;
while (fill_line(linebuf,sizeof(linebuf))) {
if (last_page != this_page) { /* local display */
printf("At Page #%-4u",this_page);
if (abs_page != this_page) printf(" (%d)",abs_page);
printf("\r");
last_page= this_page;
}
if (scan_line(linebuf) == TRUE) /* look for dot cmds */
continue;
if (this_page >= first_page + num_pages)
return; /* quit if last page */
++line_count; /* count total lines,*/
if (this_line == 0) {
do_header(); /* do headers, */
i= top_margin;
while (i--) { /* do top margin, */
lprintf("\r\n");
}
}
/* If a non blank line, do the left margin, line numbers, etc and print
the text. If a blank line, just do a line feed, for speed. */
if (strlen(linebuf) > 0) {
for (i= lmargin; i--;)
lputc(' ');
if (lineno) {
lprintf("%4u: ",line_count + 1);
this_col-= strlen(linenum);
}
lprintf("%s\r\n",linebuf);
} else lprintf("\r\n");
/* If at the bottom of the page, go to the next line. This is either
beyond the edge of the paper, or a C source and the bracket count went
to zero. */
if ((pagelen > 0) && (this_line > (pagelen - bot_margin)) || (newpage && csource)) {
while (this_line < pagelen)
lprintf("\r\n");
++this_page;
++abs_page;
this_line= 0;
newpage= 0;
}
}
}
/* Compare two strings, of a given length. Check only for equality. Return
0 if equal, else 1. */
compl(length,first,last)
int length;
char *first,*last;
{
for (; length > 0; length--)
{ if ( (toupper(*first)) != (toupper(*last)) )
return (1);
++first; ++last;
}
return (0);
}
/* Fill a line buffer with characters. Return null if end of file. */
fill_line(line,len)
char *line;
int len;
{
char d;
while (len > 0) {
d= getmc() & 0x7f; /* get a char from the file */
if (d == CR) continue;
if (d == LF) break;
if (d == '\0') break;
if (d == '{') ++brackets;
if (d == '}') if (--brackets == 0) newpage= 1;
*line++= d;
--len;
}
*line= '\0';
return(d);
}
/* Fill the memory buffer with characters from the file, or the
aux file (.in file) if open. Returns NUL when the file is empty. This
throws away nulls and control-Zs. */
getmc() {
char c;
while (1) {
if (auxfile != 0) { /* if aux file read from that */
if (abufindex >= abufcount) {
abufindex= 0;
abufcount= _xread(auxfile,auxbuf,abufsize);
if (abufcount == 0) {
_xclose(auxfile);
auxfile= 0;
continue;
}
}
c= auxbuf[abufindex++];
if ((c == NUL) || (c == SUB)) continue;
return(c);
}
if (ibufindex >= ibufcount) {
ibufindex= 0;
ibufcount= _xread(infile,inbuf,ibufsize);
if (ibufcount == 0) return(NUL);
}
c= inbuf[ibufindex++];
if ((c == SUB) || (c == NUL)) continue;
return(c);
}
}
/* Scan for dot commands. When and if we find a dot command, fool the
caller into thinking that the line we were passed is now empty. (Return TRUE)
If we find a formfeed, treat it the same as a .pa. */
scan_line(s)
char *s;
{
int n,i;
char c;
if (*s =='.') { /* must be first atom */
if (compl (3,s,".pa") == 0) {
while ((pagelen > 0) && (this_line < pagelen)) {
lprintf("\r\n");
}
++this_page;
++abs_page;
this_line= 0;
} else if (compl(3,s,".def") == 0) { /* define control char */
s= next_arg(s);
n= atoi(s);
if ((n > 0) && (n < 32)) { /* if one there, */
for (i= 0; i < CTLMAX; i++) {
s= next_arg(s); /* insert chars until */
if (! *s) break; /* end of string */
ctlstr[n][i]= atoi(s);
}
ctlstr[n][i]= '\0'; /* terminate */
}
} else if (compl (3,s,".im") == 0) { /* binary image file */
rawfile(next_arg(s));
} else if (compl (3,s,".in") == 0) {
if (auxfile == 0) { /* if one not open */
cpyarg(auxname,next_arg(s)); /* get clean filename */
auxfile= _xopen(auxname,0); /* open it, */
abufcount= 0;
abufindex= 0;
if (auxfile == -1) {
auxfile= 0; /* cant open */
cprintf("\07\r\n* Cant find .in file '%s'\r\n",auxname);
}
} else cprintf("\07\r\n* Cant nest .in files: %s from within %s\r\n",
next_arg(s),auxname);
} else if (compl (3,s,".pn") == 0) {
i= atoi(next_arg(s)); /* get page num */
if (i > 0) this_page= i; /* set if reasonable */
} else if (compl (3,s,".pl") == 0) {
pagelen= atoi(next_arg(s));
} else if (compl (3,s,".mt") == 0) {
i= atoi(next_arg(s));
if ((i > 0) && (i < 40)) /* get top margin, */
top_margin= i; /* set if reasonable */
} else if (compl (3,s,".mb") == 0) {
i= atoi(next_arg(s));
if (i < pagelen) /* set bottom margin */
bot_margin= i;
} else if (compl (3,s,".ti") == 0) {
title= 1;
} else if (compl (3,s,".he") == 0) {
strcpy(header,next_arg(s));
} else if (compl (3,s,".sh") == 0) {
strcpy(subheader,next_arg(s));
} else if (compl (3,s,".lm") == 0) {
i= atoi(next_arg(s));
if (i <80) lmargin= i;
}
return (TRUE);
} else if (*s == FF) {
if (this_page >= first_page) lputc(FF);
++this_page;
++abs_page;
this_line= 0;
return(TRUE);
}
return (FALSE);
}
/* Send a raw file to the printer. The first line should contain a number
specifying the number of lines it occupies. */
rawfile(t)
char *t;
{
int i,f;
char ln[80];
f= _xopen(t,0);
if (f != -1) {
wflush(); /* keep in sync */
_xline(f,ln,sizeof(ln));
this_line += atoi(ln); /* just assume ... */
while (i= _xread(f,ln,sizeof(ln))) {
_xwrite(outfile,ln,i);
}
_xclose(f);
}
}
/* Print the top of page header. If we find a #, replace it with
the current page number. */
do_header() {
char c;
char *p;
char pnum[82];
char date[20];
int i;
if (title) {
gtod(date);
lprintf("%-12s -- %9s %s %9s Page %d\r\n",
inname," ",date," ",this_page);
}
if (*header) hdrtxt(header);
if (*subheader) hdrtxt(subheader);
}
/* Print the header text, expanding line number (#) with left margins. */
hdrtxt(p)
char *p;
{
int i;
for (i= lmargin; i--;) lputc(' ');
while (*p) {
if (*p == '#') lprintf("%d",this_page);
else lputc(*p);
++p;
}
lprintf("\r\n");
}
/* Print to the output file. */
lprintf(f)
char *f;
{
char buf[500],*p;
_spr(buf,&f);
p= buf;
while (*p) lputc(*p++);
}
/* Write a character to the output file. This is the highest level of
character output; it does bold, underline, etc, as well as counting lines
and columns.
Write a character to the file. Count LF's as lines as they go by. Control
characters are defined by the switch() to do the normal ASCII function, but
what is actually output depends on the control character translation table. */
lputc(c)
char c;
{
char b;
int i;
if (c < ' ') {
switch(c) {
case TAB: do lputc(' '); while (this_col % 8);
c= '\0'; break;
case LF: ++this_line;
case CR: case FF: this_col= 0; break;
case BS: --this_col; break;
}
for (i= 0; ctlstr[c][i]; i++) { /* control char macros */
b= ctlstr[c][i]; /* convert 80h to NUL */
if (b == '\200') b= '\0';
lputc3(b);
}
} else {
++this_col; /* printable only */
lputc3(c);
}
}
/* Lowest level of character IO. */
lputc3(c)
char c;
{
if (this_page < first_page) return;
outbuf[obufindex++]= c;
if (obufindex >= obufsize) wflush();
}
/* Flush the output buffer. */
wflush() {
if (obufindex > 0) {
if (_xwrite(outfile,outbuf,obufindex) != obufindex) {
printf("\r\n* \07Disk Full!\r\n");
_close(outfile);
exit(1);
}
obufindex= 0;
}
}